#include "phrasecreator.h"
#include "ui_phrasecreator.h"
#include <QFileDialog>
#include <QDir>
#include <Phonon>

PhraseCreator::PhraseCreator(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::PhraseCreator)
{
    this->setModal(true);
    m_ui->setupUi(this);
    Phonon::createPath(&media, m_ui->videoWidget);
}

PhraseCreator::PhraseCreator(QWidget *parent, Phrase &p) :
    QDialog(parent),
    m_ui(new Ui::PhraseCreator)
{
    this->setModal(true);
    m_ui->setupUi(this);
    Phonon::createPath(&media, m_ui->videoWidget);

    m_ui->name->setText(p.getName());
    m_ui->difficulty->setCurrentIndex(p.getDifficulty());
    m_ui->sequence->setCurrentIndex(p.getSequence());

    m_ui->selectIcon->setIcon(QIcon(QString("icons/") + p.getIconFilename()));
    m_ui->selectIcon->setText(0);
    m_ui->selectIcon->setIconSize(QSize(90,90));
    phrase.setIconFilename(p.getIconFilename());

    m_ui->selectCartoon->setIcon(QIcon(QString("cartoons/") + p.getCartoonFilename()));
    m_ui->selectCartoon->setText(0);
    m_ui->selectCartoon->setIconSize(QSize(125,100));
    phrase.setCartoonFilename(p.getCartoonFilename());

    phrase.setVideoFilename(p.getVideoFilename());
    media.setCurrentSource(Phonon::MediaSource(QString("videos/") + p.getVideoFilename()));
    media.play();

    foreach (QString synonym, p.getSynonyms())
    {
        m_ui->synonyms->addSynonym(synonym);
    }
}

void PhraseCreator::addingSynonym()
{
    m_ui->synonyms->addSynonym(m_ui->extraSynonym->text());
}

PhraseCreator::~PhraseCreator()
{
    delete m_ui;
}

Phrase& PhraseCreator::getPhrase()
{
    this->exec();
    return phrase;
}

void PhraseCreator::selectCartoon()
{

    QString fname;
    fname = QFileDialog::getOpenFileName(
            this,
            tr("Open Preview Image"),
            QString("cartoons/"),
            tr("Images (*.png *.xpm *.jpg)"));
    if (fname == "") return;
    m_ui->selectCartoon->setIcon(QIcon(fname));
    m_ui->selectCartoon->setText(0);
    m_ui->selectCartoon->setIconSize(QSize(125,100));
    // filename relative to /cartoons
    phrase.setCartoonFilename(QDir("./cartoons").relativeFilePath(fname));
}

void PhraseCreator::selectIcon()
{

    QString fname;
    fname = QFileDialog::getOpenFileName(
            this,
            tr("Open Icon"),
            QString("icons/"),
            tr("Images (*.png *.xpm *.jpg)"));
    if (fname == "") return;
    m_ui->selectIcon->setIcon(QIcon(fname));
    m_ui->selectIcon->setText(0);
    m_ui->selectIcon->setIconSize(QSize(90,90));
    // filename relative to /icons
    phrase.setIconFilename(QDir("./icons").relativeFilePath(fname));
}

void PhraseCreator::selectVideo()
{

    QString fname;
    fname = QFileDialog::getOpenFileName(
            this,
            tr("Open Video"),
            QString("videos/"),
            tr("Videos (*.mpg *.wmv)"));
    if (fname == "") return;
    // filename relative to /videos
    phrase.setVideoFilename(QDir("./videos").relativeFilePath(fname));
    media.setCurrentSource(Phonon::MediaSource(fname));
    media.play();
}

void PhraseCreator::saveOnExit()
{
    if (m_ui->name->text() != "" && phrase.getVideoFilename() != "")
    {
        phrase.setDifficulty(m_ui->difficulty->currentIndex());
        phrase.setSequence(m_ui->sequence->currentIndex());
        phrase.setName(m_ui->name->text());
        phrase.setSynonyms(m_ui->synonyms->getCheckedSynonyms());
    }
}

void PhraseCreator::dontSaveOnExit()
{
    phrase = Phrase();
}

void PhraseCreator::updateSynonyms()
{
    m_ui->synonyms->fetch(m_ui->name->text());
}

void PhraseCreator::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
